package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.appwidget.PreUtils;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.service.LoginDownDataService;
import com.mission.schedule.utils.SharedPrefUtil;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * 登录下载对话框(登录使用)
 * Created by liwei on 2017/4/18.
 */

public class LoginDownServiceDialog extends Dialog {

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    private AVLoadingIndicatorView initialize_pr, downperson_pr, downcalender_pr, setcalender_pr;
    private ImageView initialize_im, downperson_im, downcalender_im, setcalender_im;
    private TextView t1, t2, t3, t4;
    //0 初始状态 ,1 成功 ,-1 失败
    private int initialize = 0, downperson = 0, downcalender = 0, setcalender = 0;
    LoginDownDataReceiver loginDownDataReceiver;

    public LoginDownServiceDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;


        setCancelable(false);

        View view = LayoutInflater.from(context).inflate(R.layout.server_down_local_dialog, null);
        setContentView(view);


        initialize_pr = view.findViewById(R.id.init_av);
        downperson_pr = view.findViewById(R.id.downp_av);
        downcalender_pr = view.findViewById(R.id.downc_av);
        setcalender_pr = view.findViewById(R.id.setc_av);

        new Thread(new Runnable() {
            @Override
            public void run() {
                initialize_pr.show();
                downperson_pr.show();
                downcalender_pr.show();
                setcalender_pr.show();
            }
        }).start();

        initialize_im = view.findViewById(R.id.initialize_im);
        downperson_im = view.findViewById(R.id.downperson_im);
        downcalender_im = view.findViewById(R.id.downcalender_im);
        setcalender_im = view.findViewById(R.id.setcalender_im);

        t1 = view.findViewById(R.id.t1);
        t2 = view.findViewById(R.id.t2);
        t3 = view.findViewById(R.id.t3);
        t4 = view.findViewById(R.id.t4);

        t1.setText("初始化数据");
        t2.setText("下载个人设置");
        t3.setText("下载个人数据");
        t4.setText("设置闹钟");

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        loginDownDataReceiver = new LoginDownDataReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("LoginDownDataReceiver");
        context.registerReceiver(loginDownDataReceiver, intentFilter);


        Intent LocalSyni = new Intent(context, LoginDownDataService.class);
        LocalSyni.setAction("LoginDownDataService");
        LocalSyni.setPackage(context.getPackageName());
        context.startService(LocalSyni);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.alpha = 0.52f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);
    }

    class LoginDownDataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            if (intent != null) {
                if (intent.getStringExtra("personimage") != null) {
                    initialize = -1;
                    if (intent.getStringExtra("personimage").equals("personimage")) {
                        initialize = 1;
                    } else {
                        initialize = -1;
                    }
                    initialize_pr.setVisibility(View.GONE);
                    initialize_im.setVisibility(View.VISIBLE);
                    downperson_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("downperson") != null) {
                    initialize_pr.setVisibility(View.GONE);
                    initialize_im.setVisibility(View.VISIBLE);
                    if (initialize != 1) {
                        initialize = -1;
                    }
                    if (intent.getStringExtra("downperson").equals("downperson")) {
                        downperson = 1;
                    } else {
                        downperson = -1;
                    }
                    downperson_pr.setVisibility(View.GONE);
                    downperson_im.setVisibility(View.VISIBLE);
                    downcalender_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("downData") != null) {
                    downperson_pr.setVisibility(View.GONE);
                    downperson_im.setVisibility(View.VISIBLE);
                    if (downperson != 1) {
                        downperson = -1;
                    }
                    if (intent.getStringExtra("downData").equals("downData")) {
                        downcalender = 1;
                    } else {
                        downcalender = -1;
                    }
                    downcalender_pr.setVisibility(View.GONE);
                    downcalender_im.setVisibility(View.VISIBLE);
                    setcalender_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("setData") != null) {
                    downcalender_pr.setVisibility(View.GONE);
                    downcalender_im.setVisibility(View.VISIBLE);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            if (downcalender != 1) {
                                downcalender = -1;
                            }
                            if (intent.getStringExtra("setData").equals("setData")) {
                                setcalender = 1;
                            } else {
                                setcalender = -1;
                            }
                            setcalender_pr.setVisibility(View.GONE);
                            setcalender_im.setVisibility(View.VISIBLE);
                            dismissDialog();
                            if (!ifLoginDownData) {
                                LoginDownErrorServiceDialog loginDownErrorServiceDialog = new LoginDownErrorServiceDialog(context, R.style.dialog_translucent, windowManager);
                                loginDownErrorServiceDialog.show();
                            }

                        }
                    }, 10000);
                }
                if (initialize == -1) {
                    ifLoginDownData = false;
                    initialize_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (downperson == -1) {
                    ifLoginDownData = false;
                    downperson_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (downcalender == -1) {
                    ifLoginDownData = false;
                    downcalender_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (setcalender == -1) {
                    ifLoginDownData = false;
                    setcalender_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                }
            } else {
                dismissDialog();
            }
        }
    }

    //下载数据是否异常
    boolean ifLoginDownData = true;

    private void dismissDialog() {
        if (loginDownDataReceiver != null) {
            try {
                context.unregisterReceiver(loginDownDataReceiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PreUtils.RefreshAppWidget(context);
        dismiss();
    }

}
